<?php
namespace  app\models;

class ApiAuth extends \yii\base\BaseObject implements \yii\web\IdentityInterface
{
    public $authId;
    public $username;
    public $password;
    public $authKey;
    public $accessToken;


    private static $auths = [
        '100' => [
            'authId' => '100',
            'username' => 'admin',
            'password' => 'admin',
            'authKey' => 'test100key',
            'accessToken' => '100-token',
        ],
        '101' => [
            'authId' => '101',
            'username' => 'demo',
            'password' => 'demo',
            'authKey' => 'test101key',
            'accessToken' => '101-token',
        ],
    ];

    /**
     * {@inheritdoc}
     */
    public static function findIdentity($id)
    {
        return isset(self::$auths[$id]) ? new static(self::$auths[$id]) : null;
    }

    /**
     * {@inheritdoc}
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {

        foreach (self::$auths as $auth) {
            if ($auth['accessToken'] === $token) {
                return new static($auth);
            }
        }

        return null;
    }


    /**
     * {@inheritdoc}
     */
    public function getId()
    {
        return $this->authId;
    }

    /**
     * {@inheritdoc}
     */
    public function getAuthKey()
    {
        return $this->authKey;
    }

    /**
     * {@inheritdoc}
     */
    public function validateAuthKey($authKey)
    {
        return $this->authKey === $authKey;
    }

}